package io

import (
	"time"

	"github.com/gorilla/websocket"
)

func WbRead(conn *websocket.Conn) ([]byte, error) {
	_, message, err := conn.ReadMessage()
	if err != nil {
		return nil, err
	}
	return message, nil
}

func WbReadByTime(conn *websocket.Conn, t time.Time) ([]byte, error) {
	err := conn.SetReadDeadline(t)
	if err != nil {
		return nil, err
	}
	defer conn.SetReadDeadline(time.Time{})
	_, message, err := conn.ReadMessage()
	if err != nil {
		return nil, err
	}

	return message, nil
}

func WbWrite(conn *websocket.Conn, message []byte) error {
	err := conn.WriteMessage(websocket.BinaryMessage, message)
	if err != nil {
		return err
	}
	return nil
}

func WbWriteByTime(conn *websocket.Conn, message []byte, t time.Time) error {
	err := conn.SetWriteDeadline(t)
	if err != nil {
		return err
	}
	defer conn.SetWriteDeadline(time.Time{})

	err = conn.WriteMessage(websocket.BinaryMessage, message)
	if err != nil {
		return err
	}
	return nil
}
